<?php
/*
 * Cms文章管理
 */
class Libs_Cms_Document {

    protected static $_dbAdmin;
    protected static $_cache;

    protected static $tblTitleName = 'cms_document_title_';
    protected static $tblContentName = 'cms_document_content_';
    /**
     * 数据库链接
     */
    public static function loadDb() {
        self::$_dbAdmin = Db_Admin::instance();
    }
    public static function loadCacheObj(){
		self::$_cache = ME_DAL_RefreshCacheLoader::getInstance();
    }
    public static function getDocumentList($paramArr){

       if (is_array($paramArr)) {
			$options = array(
				'appId' => 0,
				'classId' => 0,
				'status'  => 3, #默认已经发布
				'hourbwn' => 0,
				'cnt'     => 10, #默认已经发布
                'orderBy' => ' order by id desc ',#order by datetm desc
			);
			$options = array_merge($options, $paramArr);
			extract($options);
		}
        $appId = (int)$appId;
        if(!$appId)return false;

        self::loadDb();
        self::loadCacheObj();
        $subSql = ' where 1 ';

        if($status) $subSql .= " and status in ({$status})  ";
        if($classId) $subSql .= " and classId in ({$classId})  ";
		if($hourbwn) $subSql .=" AND datetm > '".date('Y-m-d H:i:s',SYSTEM_TIME -($hourbwn*3600))."' ";
        //获得每个频道的记录
        $param = array(
                'db'          => self::$_dbAdmin,
                'fromSql'     => " from " . self::$tblTitleName .$appId, #from部分的语句
                'whereSql'    => $subSql,
                'orderBySql'  => $orderBy,
                'pagesize'    => $cnt,
                'page'        => 1,
                //'debug'        => 1,
                'onlyData'    => true,
        );
        $data = Libs_DBHelper::getList($param);
        if($data){
            $classInfo = self::$_cache->loadCacheObject("Cms_Class",array('appId'=>$appId));
            foreach ($data as &$d){
                $d['url'] = Libs_Cms_Document::getDocumentUrl('',$classInfo[$d['classId']]['path'], $d['id'], $d['enfile']);
            }
        }
        return $data;
    }
    /**
     * 获得文章内容
     */
    public static function getDocumentInfo($paramArr){
       if (is_array($paramArr)) {
			$options = array(
				'appId' => 0,
				'docId' => 0,
			);
			$options = array_merge($options, $paramArr);
			extract($options);
		}
        $appId = (int)$appId;
        $docId = (int)$docId;
        if(!$appId || !$docId)return false;

        self::loadDb();
        self::loadCacheObj();
        
        //获得网站APP信息
        $appInfo = self::$_cache->loadCacheObject("Global_App",array());        
        $appInfo = $appInfo[$appId];

        //获得文章基本信息
        $sql = "select * from ".self::$tblTitleName .$appId . ' where id = '. $docId;
        $docInfo = self::$_dbAdmin->getRow($sql);

        //获得文章内容
        $sql = "select content from ".self::$tblContentName .$appId . ' where documentId = '. $docId;
        $docInfo['content'] = self::$_dbAdmin->getOne($sql);
        
        //获得相关分类信息
        //频道信息
        $classInfo = self::$_cache->loadCacheObject("Cms_Class",array('appId'=>$appId));
        $classInfo = $classInfo[$docInfo['classId']];
        return array(
            'appInfo'  => $appInfo,
            'docInfo'  => $docInfo,
            'classInfo'  => $classInfo,
        );
    }


    /**
     * 发布文章
     */
    public static function publishDocument($paramArr){
       if (is_array($paramArr)) {
			$options = array(
				'output' => false,
				'appId' => 0,
				'docId' => 0,
				'set404' => 0, //删除文章
			);
			$options = array_merge($options, $paramArr);
			extract($options);
		}

        $appId = (int)$appId;
        $docId = (int)$docId;
        if(!$output || !$appId || !$docId)return false;

        //获得APP的配置信息
        $appCfg = ME_Config::get('APP','CONFIG');
        $appCfg = $appCfg[$appId];
        
        //获得文章内容
        $output->data = self::getDocumentInfo(array('appId' => $appId,'docId' => $docId));
        

        //是否开放wap站点
        if($appCfg['hasWap']){
            $tpl = "Template/{$appId}/CmsWap/Document";
            if($set404){
                $tpl = "Template/{$appId}/CmsWap/Document404";
            }
            $output->setTemplate($tpl);
            $html = $output->fetch();
            $path = self::getDocumentPath($appCfg['wapPath'],$output->data['classInfo']['path'],$docId,$output->data['docInfo']['enfile'],'wml');
            ME_File::write($html, $path);
        }

        $tpl = "Template/{$appId}/Cms/Document";
        if($set404){
            $tpl = "Template/{$appId}/Cms/Document404";
        }
        $output->setTemplate($tpl);
        $html = $output->fetch();
        $path = self::getDocumentPath($output->data['appInfo']['enName'],$output->data['classInfo']['path'],$docId,$output->data['docInfo']['enfile']);
        ME_File::write($html, $path);


        return array(
            'path' => $path,
            'url' => self::getDocumentUrl($output->data['appInfo']['enName'],$output->data['classInfo']['path'],$docId,$output->data['docInfo']['enfile']),
        );
    }



    public static function getDocumentUrl($appPath,$classPath,$docId,$docEnFile)
    {
        $folder = ceil($docId/1000);
        $docEnFile = strtolower($docEnFile);
        $fileName = $docEnFile ? "{$classPath}_{$folder}_{$docEnFile}.shtml" : "{$classPath}_{$folder}_{$docId}.shtml";
        return  "http://www.52zuomeng.com/{$fileName}";
    }

    public static function getWapDocumentUrl($appPath,$classPath,$docId,$docEnFile)
    {
        $folder = ceil($docId/1000);
        $docEnFile = strtolower($docEnFile);
        $fileName = $docEnFile ? "{$classPath}_{$folder}_{$docEnFile}.wml" : "{$classPath}_{$folder}_{$docId}.wml";
        return  "/{$fileName}";
    }
    public static function getDocumentPath($appPath,$classPath,$docId,$docEnFile,$extName='shtml')
    {
        $docEnFile = strtolower($docEnFile);
        $fileName = $docEnFile ? "{$docEnFile}" : "{$docId}";
        return PRODUCTION_ROOT . "/Html/{$appPath}/{$classPath}/" . ceil($docId/1000) . "/{$fileName}.{$extName}";
    }

}

?>
